// 这个waveform是AudioRecord读入的原始声音pcm数据
// byte[] buffer = new byte[bufferSize];
// int length = audioRecord.read(buffer, 0, bufferSize);
public void setWaveform(byte[] waveform) {
    if (null == waveform) {
        mWaveform = null;
    } else {
        mWaveform = Arrays.copyOf(waveform, waveform.length); // 数组复制
    }

    invalidate(); // 设置波纹之后, 需要重绘
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    float width = canvas.getWidth();
    float height = canvas.getHeight();

    if (mWaveform != null) {
        renderWaveform(mWaveform, canvas);        // 绘制波形
        renderBlank(width, height, canvas);       // 绘制直线
    } else {
        renderBlank(width, height, canvas);       // 绘制直线
    }
}

private void renderWaveform(byte[] data, Canvas canvas) {
    if (null == data)
        return;

    float diffX;
    float width = canvas.getWidth() * 1.0f;
    if (data.length > width) {
        diffX = data.length / width;
    } else {
        diffX = width / data.length;
    }
    int height = canvas.getHeight();

    Path path = new Path();
    path.moveTo(0, height / 2);

    for (int i = 0; i < data.length; i++) {
        float d = data[i];
        float stepX;
        stepX = diffX * i * 1.0f;
        float stepY = d / 2 + (height / 2.0f);
        path.lineTo(stepX, stepY);
    }
    path.moveTo(width, height / 2.0f);

    canvas.drawPath(path, paint);
}

private void renderBlank(float width, float height, Canvas canvas) {
    int y = (int) (height * 0.5f);
    Path path = new Path();
    path.moveTo(0, y);
    path.lineTo(width, y);
    canvas.drawPath(path, paint);
}